<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org">
	<head>
		<div th:replace="commons/html-header"></div>
	</head>
	<body class="skin-1">

	<div th:replace="commons/head"></div>



	<div class="main-container" id="main-container">

		<script type="text/javascript">

			try{gds.settings.check('main-container' , 'fixed')}catch(e){}

		</script>

		<div class="main-container-inner">

			<a class="menu-toggler" id="menu-toggler" href="#">

				<span class="menu-text"></span>

			</a>



			<div th:replace="admin/admin-menu"></div>

			<div class="main-content">

				<div class="breadcrumbs" id="breadcrumbs">

					<script type="text/javascript">

						try{gds.settings.check('breadcrumbs' , 'fixed')}catch(e){}

					</script>



					<ul class="breadcrumb">
						<li>
							<i class="icon-home home-icon"></i>
							<a href="#">Home</a>
						</li>
						<li>
							<a>系统管理</a>
						</li>
						<li>
							厂家管理
						</li>
					</ul><!-- .breadcrumb -->

				</div>
				<div class="page-content">

					<div class="page-header">
						<form class="form-inline" role="form">
							<div class="form-group">
								<label class="sr-only" for="c_COMPANY_NAME">公司名称</label>
								<input type="text" class="form-control" id="c_COMPANY_NAME" placeholder="请输入公司名称" />
							</div>
							<button type="button" id="btn-search" class="btn btn-sm btn-info"><i class="icon-search"></i>查询</button>
							<!--<button type="button" id="btn-add" class="btn btn-sm btn-primary pull-right"><i class="icon-plus"></i>新增厂家</button>-->
						</form>

					</div>

					<div class="row">

						<div class="col-xs-12">
							<div class="table-responsive">

								<table id="tbl" class="table table-striped table-bordered table-hover">
									<thead>
									<tr>

										<th class="center">序号</th>
										<th>公司名称</th>
										<th>联系人</th>
										<th>手机号码</th>
										<th>邮箱</th>
										<th>描述</th>
										<th></th>
									</tr>
									</thead>
									<tbody>

									</tbody>
								</table>
							</div><!-- /.table-responsive -->
						</div><!-- /.col -->

					</div><!-- /.row -->

				</div><!-- /.page-content -->

			</div><!-- /.main-content -->

		</div><!-- /.main-container-inner -->

		<div class="modal fade" id="edit_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title" id="myModalLabel">
						</h4>
					</div>
					<div class="modal-body">
						<form name="edit_form" action="/company/save" method="post" class="form-horizontal">
							<input class="form-control" name="c_COMPANY_ID" autocomplete="off" type="hidden" id="md_C_COMPANY_ID" />
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_COMPANY_NAME">公司名称</label>
								<div class="col-sm-9">
									<input class="form-control" name="c_COMPANY_NAME" type="text" id="md_C_COMPANY_NAME" placeholder="请输入公司名称" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_LINK_MAN">联系人</label>
								<div class="col-sm-9">
									<input class="form-control" name="c_LINK_MAN" type="text" id="md_C_LINK_MAN" placeholder="请输入联系人" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_LINK_TEL">手机号码</label>
								<div class="col-sm-9">
									<input class="form-control" name="c_LINK_TEL" type="text" id="md_C_LINK_TEL" placeholder="请输入手机号码" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_LINK_EMAIL">邮箱地址</label>
								<div class="col-sm-9">
									<input class="form-control" name="c_LINK_EMAIL" type="text" id="md_C_LINK_EMAIL" placeholder="请输入邮箱地址" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_COMPANY_REMARK">公司描述</label>
								<div class="col-sm-9">
									<textarea style="resize: none;" rows="6" class="form-control" type="text" name="c_COMPANY_REMARK" id="md_C_COMPANY_REMARK" placeholder="请输入公司描述"/>
								</div>
							</div>
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">取消
						</button>
						<button type="button" id="btn-save" class="btn btn-sm btn-primary">
							保存
						</button>
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>

		<a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
			<i class="icon-double-angle-up icon-only bigger-110"></i>
		</a>

	</div><!-- /.main-container -->



	<!-- basic scripts -->
	<div th:replace="commons/html-js"></div>

	<!-- inline scripts related to this page -->

	<script>
		var c_COMPANY_NAME;
		var dataList;
		var button_edit = '<button type="button" class="btn btn-xs btn-info" onclick="updateCom(\'{{0}}\')" >' +
            '<i class="icon-edit bigger-120"></i>修改 ' +
            '</button> ';
		var button_del = '<button type="button" class="btn btn-xs btn-danger" onclick="delCom(\'{{0}}\')" > ' +
            '<i class="icon-trash bigger-120"></i>删除 ' +
            '</button> ' ;
		var lastTD ='<td><div class="visible-md visible-lg hidden-sm hidden-xs btn-group">';
		var lastTDEnd = '</div></td>';
		queryForList = function() {
		    $.post(contextPath + "/company/list" , {c_COMPANY_NAME : c_COMPANY_NAME} , function (data) {
		        var html = '';
		        //<![CDATA[
				if (null != data && data.length > 0) {
                    dataList = data;
				   for (var i = 0; i<data.length; i ++) {
                       html += '<tr>';
				       html += ('' +
					   '<td class="center">' + (i+1) + '</td>' +
					   '<td><div title="' + data[i].c_COMPANY_NAME + '">' + data[i].c_COMPANY_NAME + '</div></td>' +
					   '<td><div title="' + data[i].c_LINK_MAN + '">' + data[i].c_LINK_MAN + '</div></td>' +
					   '<td><div title="' + data[i].c_LINK_TEL + '">' + data[i].c_LINK_TEL + '</div></td>' +
					   '<td><div title="' + data[i].c_LINK_EMAIL + '">' + data[i].c_LINK_EMAIL + '</div></td>' +
					   '<td><div title="' + (null == data[i].c_COMPANY_REMARK ? '' : data[i].c_COMPANY_REMARK) + '">' + (null == data[i].c_COMPANY_REMARK ? '' : data[i].c_COMPANY_REMARK) + '</div></td>');
				       html += lastTD;
				       html += button_edit.replace(/\{\{0\}\}/gi,data[i].c_COMPANY_ID);
				       if (0 == data[i].c_IS_DEFAULT) {
                           html += button_del.replace(/\{\{0\}\}/gi,data[i].c_COMPANY_ID);
					   }
				       html += lastTDEnd;
                       html += '</tr>';
				   }

				}
				// ]]>
				$("#tbl tbody").html(html);
            })
		}
		initEditModal = function (title,id,name,linkman, tel, email,remark) {
            $("#md_C_COMPANY_ID").val(id);
            $("#md_C_COMPANY_NAME").val(name);
            $("#md_C_LINK_MAN").val(linkman);
            $("#md_C_LINK_TEL").val(tel);
            $("#md_C_LINK_EMAIL").val(email);
            $("#md_C_COMPANY_REMARK").val(remark);
            $("#myModalLabel").text(title);
            $("#edit_modal").modal();
        }
        updateCom = function (c_COMPANY_ID) {
			var companyName,linkMan,linkTel,linkEmail;
			var companyRemark;
			// <![CDATA[
			if (null != dataList && dataList.length > 0)
			$(dataList).each(function(index, obj) {
			    if (obj.c_COMPANY_ID == c_COMPANY_ID) {
                    companyName = obj.c_COMPANY_NAME;
                    linkMan = obj.c_LINK_MAN;
                    linkTel = obj.c_LINK_TEL;
                    linkEmail = obj.c_LINK_EMAIL;
                    companyRemark = obj.c_COMPANY_REMARK;
				}
			});
			if (null != companyName ) {
			    this.initEditModal("修改厂家信息",c_COMPANY_ID,companyName,linkMan,linkTel,linkEmail, companyRemark);
			}
			// ]]>
        }
        delCom = function (c_COMPANY_ID) {
//		    <![CDATA[
			bootbox.confirm({
                title : "<i class='icon-question-sign'></i> 确认",
			    message :'确定删除厂家？',
                size: 'small',
				buttons:{
                    confirm: {
                        label: "确定",
						className: 'btn-sm btn-primary'
                    },
                    cancel:{
                        label:"取消",
                        className: 'btn-sm btn-default'
                    }
				},
				callback : function (result) {
                    if (result) {
                        $.post(contextPath + "/company/" + c_COMPANY_ID + "/delete", function (data) {
                            var ok = false;
                            if (null != data) {
                                if (0 == data.code) {
                                    queryForList();
                                    ok = true;
                                }
                            }
                            if (!ok) {
                                bootbox.dialog({
                                    message: '<span class="bigger-110">删除失败</span>',
                                    buttons: {
                                        "danger": {
                                            "label": "确定",
                                            "className": "btn-sm btn-danger"
                                        }
                                    }
                                });
                            }
                        });
                    }
                }
			});

//            ]]>
        }
		$(document).ready(function () {
		    queryForList(null);
			$("#btn-search").on("click" , function () {
                c_COMPANY_NAME = $("#c_COMPANY_NAME").val();
				queryForList();
            });
            $("#btn-save").on("click",function () {
                $.post(contextPath + '/company/save',$("form[name=edit_form]").serializeArray(), function (data) {
                        //<![CDATA[
                        if (null != data) {
                            if (0 == data.code) {
                                $("#edit_modal").modal('hide');
                                queryForList()
                            } else if ( 500 == data.code) {
                                bootbox.dialog({
                                    message: '<span class="bigger-110">' + data.msg + '</span>',
                                    buttons:
                                        {
                                            "danger" :
                                                {
                                                    "label" : "确定",
                                                    "className" : "btn-sm btn-danger"
                                                }
                                        }
                                });
							}
                        }
                        return false;
                        // ]]>
                    });
                return false;
            });

        });
	</script>
</body>

</html>

